﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SP.OAWeb.WebApi.DTO;
using Dapper;
using MySql.Data.MySqlClient;

namespace SP.OAWeb.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        //框架中给我们提供的接口，该接口的作用是获取APPsetting.json配置
        private IConfiguration _configuration;
        public DemoController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 登录功能
        /// </summary>
        /// <param name="logindto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Login")]
        public IActionResult Login(LoginDTO logindto)
        {
            string conns = _configuration.GetConnectionString("default");//获取连接字符串的配置内容
            //using的作用是确保 db资源在用完之后会被自动释放
            using var db = new MySqlConnection(conns);
            //定义一个SQL语句字符串
            string sql = "select count(1) from Uerinfo where Username=@name and Password=@pass";
            //使用Dapper的扩展方法ExecuteScalar来获取SQL的执行结果
            int result = db.ExecuteScalar<int>(sql, new { name = logindto.UserName, pass = logindto.Password });
            return Ok(result);
        }
    }
}
